package com.cqjtu.controller;

import com.cqjtu.pojo.PageBean;
import com.cqjtu.pojo.Result;
import com.cqjtu.pojo.Teams;
import com.cqjtu.pojo.Worker;
import com.cqjtu.service.TeamsService;
import com.cqjtu.service.WorkerService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Slf4j
public class TeamsController {

    @Autowired
    private TeamsService teamsService;
    @Autowired
    private WorkerService workerService;

    //新增班组
    @PostMapping("/teams/insert")
    public Result add(@RequestBody Teams teams){
        log.info("新增员工，worker:{}",teams);
        teamsService.add(teams);
        return Result.success();
    }

    //删除班组
    @DeleteMapping("/teams/{teams_id}")
    public Result delete(@PathVariable List<Integer> teams_id){
        log.info("删除操作，ids:{}",teams_id);
        teamsService.delete(teams_id);
        workerService.delete(teams_id);
        return Result.success();
    }

    //班组条件查询（通过班组名称，承包人，班组人数查询）
    @GetMapping("/teams")
    public Result page(@RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "10") Integer pageSize,
                       String teamsName,String contractor,Integer teamsNumber,Integer n){
        log.info("分页查询，参数：{},{},{},{},{},{}",page,pageSize,teamsName,contractor,teamsNumber,n);
        //调用service分页查询
        PageBean pageBean = teamsService.page(page,pageSize,teamsName,contractor,teamsNumber,n);
        return Result.success(pageBean);
    }

    //班组查询（通过id）
    @GetMapping("/teamsinfo/{teamsId}")
    public Result getById(@PathVariable Integer teamsId){
        log.info("根据班组id查询：teamsId{}",teamsId);
        Teams teams = teamsService.getById(teamsId);
        return Result.success(teams);
    }

    //班组更新
    @PutMapping("/teamsinfo")
    public Result update(@RequestBody Teams teams){
        log.info("更新班组信息：{}",teams);
        teamsService.update(teams);
        return Result.success();
    }



}
